home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 3 / CD ACTUAL 3.iso / linux / docs / linux-do / programm / lpg-0.4 / lpg-0 / LPG / examples / input.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-17  |  2.9 KB  |  94 lines

  1. /*
  2.  * INP.C - A demonstration program for ncurses.
  3.  *         This program is originally distributed as a part of
  4.  *         the <Linux Programmers Guide>
  5.  *
  6.  *  AUTHOR: Sven van der Meer (vdmeer@cs.tu-berlin.de)
  7.  *
  8.  *  This program is free software; you can redistribute it and/or
  9.  *  modify it under the terms of the GNU General Public License as
  10.  *  published by the Free Software Foundation; either version 2 of
  11.  *  the License,or (at your option) any later version.
  12.  *
  13.  *  This program is distributed in the hope that it will be useful,
  14.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  *  GNU General Public License for more details.
  17.  *
  18.  *  You should have received a copy of the GNU General Public License
  19.  *  along with this program; if not, write to the Free Software
  20.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23. #include <ncurses/ncurses.h>  /* always needed */
  24. #include <stdlib.h>   /* malloc is defined there */
  25.  
  26. void vdmerase (WINDOW *); /* for erasing the screen, werase has some probs */
  27.  
  28. WINDOW *maskwin;
  29. WINDOW *findwin;
  30.  
  31. void main()
  32. {
  33. char *ptr=(char *)malloc(255); /* the pointer and 255 Byte of memory for it */
  34.  
  35.  if(!(stdscr=initscr())){  /* don't forget this!!! */
  36.    fprintf(stderr,"inp: initscr() failed\n\n");
  37.    exit (1);
  38.  }
  39.  if (!(findwin=newwin(3,37,9,21))){  /* open a window with error check */
  40.    fprintf(stderr,"inp: can't open findwin\n\n");
  41.    endwin(); exit (1);
  42.  }
  43.  if (!(maskwin=newwin(1,21,10,35))){  /* open a window with error check */
  44.    fprintf(stderr,"inp: can't open maskwin\n\n");
  45.    endwin(); exit (1);
  46.  }
  47.  
  48.  start_color();  /* check for color and initialize the color or attributes */
  49.  if (has_colors()){
  50.    init_pair(1,COLOR_WHITE,COLOR_RED);
  51.    init_pair(2,COLOR_BLACK,COLOR_WHITE);
  52.    wattrset(findwin,COLOR_PAIR(2));
  53.    wattrset(maskwin,COLOR_PAIR(1));
  54.  }
  55.  else{
  56.    wattrset(findwin,A_REVERSE);
  57.    wattrset(maskwin,A_NORMAL);
  58.  }
  59.  vdmerase(findwin); /* erase the windows, this will show the colors, too */
  60.  vdmerase(maskwin);
  61.  
  62.  box(findwin,0,0);  /* draw a box around the window */
  63.  mvwaddstr(findwin,1,2,"Inputstring: "); /* print this to the window */
  64.  
  65.  keypad(stdscr,TRUE);  /* enable keypad and cursor keys */
  66.  timeout(-1);          /* don't wait on input */
  67.  nonl();               /* no newline translation */
  68.  
  69.  wnoutrefresh(findwin);
  70.  wnoutrefresh(maskwin);
  71.  doupdate();  /* show the windows */
  72.  
  73.  mvwgetstr(maskwin,0,0,ptr); /* wait for input */
  74.  
  75.  delwin(maskwin); /* delete the windows and free up the resources */
  76.  delwin(findwin);
  77.  endwin();  /* end ncurses */
  78.  printf("The input was: %s\n",ptr); /* print out the input */
  79.  free(ptr); /* free up the ptr */
  80.  return; /* exit */
  81. }
  82.  
  83. void vdmerase (WINDOW *win)
  84. {
  85. int  y,x;
  86.  
  87.  for (y=0;y<=win->_maxy;y++)
  88.    for (x=0;x<=win->_maxx;x++)
  89.      (chtype *)win->_line[y][x]=' '|win->_attrs;
  90.  win->_curx = win->_cury = 0;
  91.  touchwin(win);
  92. return;
  93. }
  94.